package com.asset.dispoal.nature.service.commom;

import com.asset.dispoal.commom.page.PagingQuery;
import com.asset.dispoal.nature.domain.db.entity.GbNature;
import com.asset.dispoal.nature.domain.db.mapper.GbNatureMapper;
import com.asset.dispoal.nature.dto.requset.GbNatureRequest;
import com.asset.dispoal.nature.dto.response.GbNatureResponse;
import com.asset.dispoal.nature.service.converter.GbNatureConverter;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import tech.mhuang.pacebox.core.ddd.BaseCommand;
import tech.mhuang.pacebox.springboot.protocol.Result;
import tech.mhuang.pacebox.springboot.protocol.data.Page;
import tech.mhuang.pacebox.springboot.protocol.data.PageVO;

import java.util.List;

/**
 * @author qr
 * @date 2022/2/28$
 */
@Component
public class QueryPageNatureCommand implements BaseCommand<GbNatureRequest, Result> {


    @Autowired
    GbNatureMapper gbNatureMapper;

    @Override
    public Result<PageVO<GbNatureResponse>> execute(GbNatureRequest request) {

        final GbNature gbNature = GbNatureConverter.INSTANCE.reqToDo(request);
        final int count = gbNatureMapper.pageCount(gbNature);
        final Page<GbNature> page = PagingQuery.getPage(count, gbNature);
        PageVO<GbNatureResponse> gbNatureResponsePageVO = new PageVO<>();
        if (page != null) {
            final List<GbNature> gbNatureList = gbNatureMapper.page(page);
            final List<GbNatureResponse> gbNatureResponses = GbNatureConverter.INSTANCE.dosToRes(gbNatureList);
            gbNatureResponsePageVO.setResult(gbNatureResponses);
        }
        gbNatureResponsePageVO.setTotalSize(count);

        return Result.ok(gbNatureResponsePageVO);
    }
}
